package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.FKParamConfigRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.PayConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class FKParamConfigController {

    @Autowired
    private PayConfigClientService payConfigClientService;

    /*
     * @Description:获取商户风控参数
     * @Author: lu 
     * @Date: 2022/7/7 14:05
     * @param jsonObject:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(WebApiServiceUrl.URL_MerchPageGetFkConfig)
    public Result merchPageGetFkConfig(@RequestBody JSONObject jsonObject){
        try {
            Result resultUserFkConfig=payConfigClientService.getUserFkConfig(jsonObject.getLong("comId"), jsonObject.getInteger("merchId"));
            if (resultUserFkConfig.getCode()!=200){
                return resultUserFkConfig;
            }

            Result result=payConfigClientService.getFkConfig(jsonObject.getLong("comId"), jsonObject.getInteger("merchId"));
            if (result.getCode()!=200){
                return result;
            }
            JSONObject reJson= JSON.parseObject(JSON.toJSONString(result.getData()));
            if (reJson!=null){
                reJson.put("userPara",resultUserFkConfig.getData());
            }else {
                reJson=new JSONObject();
                reJson.put("userPara",resultUserFkConfig.getData());
            }
            return ResultUtil.success(reJson);
        }catch (Exception e){
            log.info("获取商户风控参数异常:"+e);
            return ResultUtil.error(ResultEnum.Err_900);
        }
    }

    /*
     * @Description:3.2.6 按商户设置风控参数
     * @Author: lu 
     * @Date: 2022/7/7 14:10
     * @param fkParamConfigRequest:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(WebApiServiceUrl.URL_MerchPageSetFkConfig)
    public Result merchPageSetFkConfig(@RequestBody FKParamConfigRequest fkParamConfigRequest){
        try {
            fkParamConfigRequest.setAccount(SecurityUtils.getUsername());
            Result result=payConfigClientService.setFkConfig(fkParamConfigRequest);
            if (result.getCode()!=200){
                return result;
            }
            if (fkParamConfigRequest.getUserPara()!=null){
                JSONObject reJson= JSON.parseObject(JSON.toJSONString(fkParamConfigRequest));
                reJson.put("user",SecurityUtils.getUsername());
                payConfigClientService.setUserFkConfig(reJson);
            }
            return ResultUtil.success();
        }catch (Exception e){
            log.info("获取商户风控参数异常:"+e);
            return ResultUtil.error(ResultEnum.Err_900);
        }
    }

    @PostMapping(WebApiServiceUrl.URL_MerchPageDeleteFkConfig)
    public Result merchPageDeleteFkConfig(@RequestBody FKParamConfigRequest fkParamConfigRequest){
        try {
            fkParamConfigRequest.setAccount(SecurityUtils.getUsername());
            return payConfigClientService.deleteFkConfig(fkParamConfigRequest.getComId(),fkParamConfigRequest.getMerchId());
        }catch (Exception e){
            log.info("获取商户风控参数异常:"+e);
            return ResultUtil.error(ResultEnum.Err_900);
        }
    }
}
